package com.ycu.jk16.mall.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ycu.jk16.mall.domain.ShoppingCart;
import com.ycu.jk16.mall.service.ProductService;
import com.ycu.jk16.mall.service.ShoppingCartService;
import com.ycu.jk16.mall.service.UserService;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

@Controller
@RequestMapping(value = "/cart")
public class ShoppingCartController {
	
	
	@Autowired
	@Qualifier("shoppingCartService")
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	@Qualifier("userService")
	private UserService userService;
	
	@Autowired
	@Qualifier("productService")
	private ProductService productService;
	
	
	@RequestMapping(value = "/list")
	public void showShoppingCartList(Integer userId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList;
		System.out.println("list:userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			

			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);

			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);
			System.out.println("---"+totlemoney);
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			System.out.println("+++"+totlemoney);
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			e.printStackTrace();
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
		
	}
	
	@RequestMapping(value = "/add")
	public void addShoppingCart(Integer userId,Integer productId,Integer count,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList;
		System.out.println("add：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			if (productService.selectById(productId) == null) { //判断是否有该产品
				throw new Exception();
			}
			
			shoppingCartService.addShoppingCart(userId, productId, count);
			
			//返回添加商品后的购物车
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			e.printStackTrace();
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	
	@RequestMapping(value = "/update")
	public void updateShoppingCart(Integer userId,Integer productId,Integer count,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList;
		System.out.println("update ：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			shoppingCartService.updateShoppingCartOfQuantity(userId, productId, count);
			
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			e.printStackTrace();
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	
	/*
	 * 删除商品后 返回该用户的购物车
	 */
	@RequestMapping(value = "/delete_product")
	public void updateShoppingCart(Integer userId,Integer productId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList;
		System.out.println("delete_product:userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			shoppingCartService.deleteShoppingCart(userId, productId);
			
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	
	@RequestMapping(value = "/select")
	public void selectShoppingCartOfProduct(Integer userId,Integer productId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList = new ArrayList<ShoppingCart>();
		System.out.println("select:userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			ShoppingCart shoppingCart = shoppingCartService.selectShoppingCartByUserIdWithProductId(userId, productId);
			shoppingCartList.add(shoppingCart);
			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);  //选中商品的价格
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			//在dataInfo中生成cartProductVoList 字段对应的值
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	@RequestMapping(value = "/un_select")
	public void unselectShoppingCartOfProduct(Integer userId,Integer productId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList = new ArrayList<ShoppingCart>();
		System.out.println("un_select：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			ShoppingCart shoppingCart = shoppingCartService.selectShoppingCartByUserIdWithProductId(userId, productId);
			shoppingCartList.add(shoppingCart);
			BigDecimal totlemoney = BigDecimal.ZERO;  //反选中商品的价格
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			//在dataInfo中生成cartProductVoList 字段对应的值
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	
	/*
	 * 查询在购物车里的产品数量
	 */
	@RequestMapping(value = "/get_cart_product_count")
	public void getCartProductCount(Integer userId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList;
		System.out.println("get_cart_product_count：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			Integer count = shoppingCartService.allProductOfCart(shoppingCartList);
			info.put("status", 0);
			info.put("data", count); //购物车中产品的数量
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	/*
	 * 购物车全选
	 */
	@RequestMapping(value = "/select_all")
	public void selectAllOfCart(Integer userId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList = new ArrayList<ShoppingCart>();
		System.out.println("select_all：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			BigDecimal totlemoney = shoppingCartService.getPrice(shoppingCartList);  //选中商品的价格
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			//在dataInfo中生成cartProductVoList 字段对应的值
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	/*
	 * 购物车全反选
	 */
	@RequestMapping(value = "/un_select_all")
	public void unselectAllOfCart(Integer userId,HttpServletResponse response) {
		Map<String ,Object> info = new HashMap<String, Object>();
		Map<String ,Object> dataInfo = new HashMap<String, Object>();
		List<ShoppingCart> shoppingCartList = new ArrayList<ShoppingCart>();
		System.out.println("un_select_all：userId = " + userId);
		
		try{
			if (userService.findUserById(userId) == null) { //判断是否有该用户---登陆的用户  
				throw new Exception();
			}
			shoppingCartList = shoppingCartService.selectShoppingCartByUserId(userId);
			BigDecimal totlemoney = BigDecimal.ZERO;  //感觉应该是选中商品的价格相反数
			totlemoney = totlemoney.setScale(2,BigDecimal.ROUND_HALF_UP);//四舍五入保留两位小数
			
			info.put("status", 0);
//			dataInfo.put("cartProductVoList", shoppingCartList);
			//在dataInfo中生成cartProductVoList 字段对应的值
			shoppingCartService.insertCartDataToMap(dataInfo, shoppingCartList);
			dataInfo.put("allChecked", true);
			dataInfo.put("cartTotalPrice", totlemoney);
			info.put("data", dataInfo);
			
		} catch(Exception e) {
			info.clear();
			info.put("status", 10);
			info.put("msg", "用户未登录,请登录");
		} finally {
			ResponseJsonUtils.json(response, info);
		}
	}
	
	
}
